package com.wpre.lotterysystem.controller;

import com.wpre.lotterysystem.common.errcode.ControllerErrorCodeConstants;
import com.wpre.lotterysystem.common.exception.ControllerException;
import com.wpre.lotterysystem.common.pojo.CommonResult;
import com.wpre.lotterysystem.common.utils.JacksonUtil;
import com.wpre.lotterysystem.controller.param.ShortMassageLoginParam;
import com.wpre.lotterysystem.controller.param.UserPasswordLoginParam;
import com.wpre.lotterysystem.controller.param.UserRegisterParam;
import com.wpre.lotterysystem.controller.result.BaseUserInfoResult;
import com.wpre.lotterysystem.controller.result.UserLoginResult;
import com.wpre.lotterysystem.controller.result.UserRegisterResult;
import com.wpre.lotterysystem.service.UserService;
import com.wpre.lotterysystem.service.VerificationCodeService;
import com.wpre.lotterysystem.service.dto.UserDTO;
import com.wpre.lotterysystem.service.dto.UserLoginDTO;
import com.wpre.lotterysystem.service.dto.UserRegisterDTO;
import com.wpre.lotterysystem.service.enums.UserIdentityEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-11
 * Time: 16:21
 */
@Slf4j
@RestController
public class UserController {

    //    @Autowired
    @Resource(name = "userServiceImpl")
    private UserService userService;

    @Resource(name = "verificationCodeServiceImpl")
    private VerificationCodeService verificationCodeService;


    /**
     * 注册
     */
    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(@RequestBody @Validated UserRegisterParam param) {
        //RequestBody: 我们希望前端通过json进行参数传递,使用RequestBody可以接收前端的json数据

        //日志打印
        log.info("userRegister-UserRegisterParam:{}", JacksonUtil.writeValueAsString(param));

        UserRegisterDTO registerDTO = userService.register(param);

        return CommonResult.success(converToUserRegisterResult(registerDTO));//需要进行对象转换
    }

    /**
     * 发送验证码
     */
    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode(String phoneNumber) {
        log.info("给 {} 发送验证码", phoneNumber);
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(Boolean.TRUE);
    }


    /**
     * 密码登录方式
     *
     * @param param
     * @return
     */
    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> userPasswordLogin(
            @Validated @RequestBody UserPasswordLoginParam param) {
        log.info("userPasswordLogin UserPasswordLoginParam {}"
                , JacksonUtil.writeValueAsString(param));

        UserLoginDTO userLoginDTO = userService.login(param);

        return CommonResult.success(converToUserLoginResult(userLoginDTO));
    }


    /**
     * 短信验证码登录方式
     *
     * @param param
     * @return
     */
    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> shortMassageLogin(
            @Validated @RequestBody ShortMassageLoginParam param) {

        log.info("shortMassageLogin ShortMassageLoginParam {}"
                , JacksonUtil.writeValueAsString(param));

        UserLoginDTO userLoginDTO = userService.login(param);

        return CommonResult.success(converToUserLoginResult(userLoginDTO));
    }

    @RequestMapping("/base-user/find-list")
    public CommonResult<List<BaseUserInfoResult>> findBaseUserInfo(String identity) {
        log.info("findBaseUserInfo identity {}", identity);

        List<UserDTO> userDTOList = userService.findBaseUserInfo(UserIdentityEnum.forName(identity));
        return CommonResult.success(converToList(userDTOList));
    }

    private List<BaseUserInfoResult> converToList(List<UserDTO> userDTOList) {
        if (CollectionUtils.isEmpty(userDTOList)) {
            return Arrays.asList();
        }
        return userDTOList.stream()
                .map(userDTO -> {
                    BaseUserInfoResult result = new BaseUserInfoResult();
                    result.setUserId(userDTO.getUserId());
                    result.setUserName(userDTO.getUserName());
                    result.setIdentity(userDTO.getIdentity().name());
                    return result;
                }).collect(Collectors.toUnmodifiableList());
    }


    //UserRegisterDTO对象转成UserRegisterResult
    private UserRegisterResult converToUserRegisterResult(UserRegisterDTO registerDTO) {
        UserRegisterResult result = new UserRegisterResult();
        if (registerDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }

        result.setUserId(registerDTO.getUserId());
        return result;
    }

    //UserLoginDTO-->UserLoginResult
    private UserLoginResult converToUserLoginResult(UserLoginDTO userLoginDTO) {
        UserLoginResult result = new UserLoginResult();
        if (userLoginDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        result.setIdentity(userLoginDTO.getIdentity().name());
        result.setToken(userLoginDTO.getToken());
        return result;
    }


}
